package com.cyys.modules.system.controller.org;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.dataScope.DataScope;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.common.validate.Detail;
import com.cyys.common.validate.Insert;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import com.cyys.modules.system.model.org.OrgNotice;
import com.cyys.modules.system.service.org.OrgNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sys/orgNotice")
public class OrgNoticeAction extends BaseAction {

	@Resource
	private OrgNoticeService orgNoticeService;

	@RequestMapping(value = "/list",method = RequestMethod.GET)
	@RequiresPermissions("sys:orgNotice:menu")
	public String list(Model model) {
		model.addAttribute("roleScope", getUser().getType());
		model.addAttribute("noticeTypeBasicId", ParamConstants.NOTICE_TYPE);
		model.addAttribute("noticeTypeMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.NOTICE_TYPE));
		return "module/system/org/orgNotice-list";
	}

	@ResponseBody
	@PostMapping("/list/listInfo")
	@SystemLog(module="通知公告",methods="通知公告-查询通知公告")
	public String listInfo(OrgNotice orgNotice, Page page) throws Exception {
		return JSONData.toLayerData(orgNoticeService.selectPageByScope(page , new QueryWrapper(orgNotice),new DataScope()));
	}

	@RequestMapping(value = "/add/input",method = RequestMethod.GET)
	public String input(Model model, String orgId) throws Exception {
		String roleScope = getUser().getType();
		if("3".equals(roleScope)){
			orgId= getUser().getOrgId();
		}
		OrgNotice notice= new OrgNotice();
		notice.setOrgId(orgId);
		model.addAttribute("notice", notice);
		model.addAttribute("roleScope", roleScope);
		return "module/system/org/orgNotice-input";
	}

	@RequestMapping(value = "/edit/input",method = RequestMethod.GET)
	public String editInput(Model model, String id) throws Exception {
		model.addAttribute("notice", orgNoticeService.getById(id));
		model.addAttribute("roleScope", getUser().getType());
		model.addAttribute("noticeTypeMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.NOTICE_TYPE));
		return "module/system/org/orgNotice-input";
	}

	@ResponseBody
	@PostMapping("/add/save")
	@SystemLog(module = "通知公告", methods = "通知公告-保存通知公告")
	public AR save(@Validated(Insert.class) OrgNotice orgNotice) throws Exception {
		return orgNoticeService.save(orgNotice);
	}

	@ResponseBody
	@PostMapping("/edit/save")
	@SystemLog(module = "通知公告", methods = "通知公告-保存通知公告")
	public AR edit(@Validated(Detail.class)OrgNotice orgNotice) throws Exception {
		return orgNoticeService.update(orgNotice);
	}

	@ResponseBody
	@PostMapping("/del/deleteById")
	@SystemLog(module = "通知公告", methods = "通知公告-删除通知")
	public AR deleteById(String id) throws Exception {
		return orgNoticeService.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@SystemLog(module = "通知公告", methods = "通知公告-批量删除通知")
	public AR deleteByIds(String ids) throws Exception {
		return orgNoticeService.deleteBatchIds(ids);
	}

	@RequestMapping(value = "/userReadDetail",method = RequestMethod.GET)
	public String userReadDetail(String orgId, String noticeId, Model model) throws Exception {
		model.addAttribute("orgId",orgId);
		model.addAttribute("noticeId",noticeId);
		return "module/system/org/orgNotice-readList";
	}

	@ResponseBody
	@PostMapping("/userReadDetail/listInfo")
	public String userReadList(Page page, String orgId, String noticeId, String userName) throws Exception {
		return JSONData.toLayerData(orgNoticeService.userReadDetail(page, orgId, noticeId, userName));
	}
}
